#ifndef VTKPATHWRITER_HPP
#define VTKPATHWRITER_HPP

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "Types.hpp"
#include <vector>
#include "Debug.hpp"
#include <iostream>
#include <stdlib.h>

class VTKPathWriter
{
public:

  VTKPathWriter(const std::string &problem, const cl_uint& N_particles);

  ~VTKPathWriter( )
  {
    fprintf( mStream, "\n" );
    fclose(mStream);
  }

  void writePoints(cl_real4* const& points);

  void writeScalars(cl_real* const& scalars,const std::string &name);
    
  void writeVector(cl_real4* const& vec, const std::string &name);
  
private:
  
  FILE *mStream;
  cl_uint N_points;
  std::string problem1;
};

VTKPathWriter::VTKPathWriter(const std::string &problem, const cl_uint& N_particles):
  N_points(N_particles)
{
    std::string filename = problem;
    filename += ".vtk";
    problem1 = filename;
    mStream = fopen(filename.c_str(), "w");
    
    if ( mStream == NULL )
      ERROR("Error opening outputfile");
    
    fprintf( mStream, "# vtk DataFile Version 4.0\n");
    fprintf( mStream, "hesp visualization file\n");
    fprintf( mStream, "ASCII\n");
    fprintf( mStream, "DATASET UNSTRUCTURED_GRID\n");
#ifdef USE_DOUBLE_PRECISION
    fprintf( mStream, "POINTS %d double\n",N_points);
#else
    fprintf( mStream, "POINTS %d float\n",N_points);
#endif
}

void VTKPathWriter::writePoints(cl_real4* const& points)
{
    for(cl_uint i=0;i<N_points;++i)
      fprintf( mStream, "%f %f %f\n", points[i].s[0],points[i].s[1],points[i].s[2]);
    fprintf( mStream, "CELLS 0 0\n");
    fprintf( mStream, "CELL_TYPES 0\n");
}

void VTKPathWriter::writeScalars(cl_real* const& scalars,const std::string &name)
{
    fprintf( mStream, "POINT_DATA %d\n",N_points);
#ifdef USE_DOUBLE_PRECISION
    fprintf( mStream, "SCALARS %s double\n",name.c_str());
#else
    fprintf( mStream, "SCALARS %s float\n",name.c_str());
#endif
    fprintf( mStream, "LOOKUP_TABLE default\n");
    for(cl_uint i=0;i<N_points;++i)
      fprintf( mStream, "%f\n", scalars[i]);
}

void VTKPathWriter::writeVector(cl_real4* const& vec, const std::string &name)
{
#ifdef USE_DOUBLE_PRECISION
    fprintf( mStream, "VECTORS %s double\n", name.c_str()); 
#else
    fprintf( mStream, "VECTORS %s float\n", name.c_str()); 
#endif
    for (cl_uint i=0; i < N_points; ++i)
      fprintf( mStream, "%f %f %f\n", vec[i].s[0], vec[i].s[1], vec[i].s[2]);
}


#endif // VTKPATHWRITER_HPP



